package com.wutu.nbyjk8.safe;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Author : Liuts
 * @Desc : 将http请求中的request替换成自定义request,方便body中数据复制
 * @create 2023/12/28 16:36
 */
@Component
public class ReplaceStreamFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        String contentType = httpServletRequest.getContentType();
        if (contentType != null && contentType.startsWith("multipart/form-data")) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            filterChain.doFilter(new RequestWrapper((HttpServletRequest) servletRequest), servletResponse);
        }
    }
}
